"""
阈值验证器
用于验证输入参数是否符合阈值要求
"""

from typing import Any, Dict, List, Optional
from config.thresholds import (
    RAGThresholds,
    KnowledgeBaseThresholds,
    EmbeddingThresholds,
    ModelThresholds,
    SecurityThresholds,
    ValidationThresholds
)

class ThresholdValidator:
    """阈值验证器"""
    
    @staticmethod
    def validate_rerank_threshold(threshold: float) -> bool:
        """验证重排序阈值"""
        return RAGThresholds.MIN_RERANK_THRESHOLD <= threshold <= RAGThresholds.MAX_RERANK_THRESHOLD
    
    @staticmethod
    def validate_top_k(top_k: int) -> bool:
        """验证top_k参数"""
        return RAGThresholds.MIN_TOP_K <= top_k <= RAGThresholds.MAX_TOP_K
    
    @staticmethod
    def validate_chunk_size(chunk_size: int) -> bool:
        """验证文本块大小"""
        return KnowledgeBaseThresholds.MIN_CHUNK_SIZE <= chunk_size <= KnowledgeBaseThresholds.MAX_CHUNK_SIZE
    
    @staticmethod
    def validate_chunk_overlap(chunk_overlap: int) -> bool:
        """验证文本块重叠"""
        return KnowledgeBaseThresholds.MIN_CHUNK_OVERLAP <= chunk_overlap <= KnowledgeBaseThresholds.MAX_CHUNK_OVERLAP
    
    @staticmethod
    def validate_temperature(temperature: float) -> bool:
        """验证温度参数"""
        return ModelThresholds.MIN_TEMPERATURE <= temperature <= ModelThresholds.MAX_TEMPERATURE
    
    @staticmethod
    def validate_max_tokens(max_tokens: int) -> bool:
        """验证最大token数"""
        return ModelThresholds.MIN_MAX_TOKENS <= max_tokens <= ModelThresholds.MAX_MAX_TOKENS
    
    @staticmethod
    def validate_text_length(text: str) -> bool:
        """验证文本长度"""
        length = len(text)
        return EmbeddingThresholds.MIN_TEXT_LENGTH <= length <= EmbeddingThresholds.MAX_TEXT_LENGTH
    
    @staticmethod
    def validate_name_length(name: str) -> bool:
        """验证名称长度"""
        length = len(name)
        return ValidationThresholds.MIN_NAME_LENGTH <= length <= ValidationThresholds.MAX_NAME_LENGTH
    
    @staticmethod
    def validate_description_length(description: str) -> bool:
        """验证描述长度"""
        length = len(description)
        return length <= ValidationThresholds.MAX_DESCRIPTION_LENGTH
    
    @staticmethod
    def validate_collection_name_length(name: str) -> bool:
        """验证集合名称长度"""
        length = len(name)
        return ValidationThresholds.MIN_COLLECTION_NAME_LENGTH <= length <= ValidationThresholds.MAX_COLLECTION_NAME_LENGTH
    
    @staticmethod
    def validate_file_extension(filename: str) -> bool:
        """验证文件扩展名"""
        import os
        _, ext = os.path.splitext(filename.lower())
        return ext in SecurityThresholds.ALLOWED_FILE_EXTENSIONS
    
    @staticmethod
    def validate_file_size(file_size_bytes: int) -> bool:
        """验证文件大小"""
        max_size_bytes = SecurityThresholds.MAX_FILE_CONTENT_LENGTH
        min_size_bytes = SecurityThresholds.MIN_FILE_SIZE_KB * 1024
        return min_size_bytes <= file_size_bytes <= max_size_bytes
    
    @staticmethod
    def validate_batch_size(batch_size: int) -> bool:
        """验证批量大小"""
        return 1 <= batch_size <= EmbeddingThresholds.MAX_BATCH_SIZE

class ValidationError(Exception):
    """验证错误异常"""
    pass

class ThresholdValidatorWithError:
    """带错误信息的阈值验证器"""
    
    @staticmethod
    def validate_rerank_threshold(threshold: float) -> None:
        """验证重排序阈值"""
        if not ThresholdValidator.validate_rerank_threshold(threshold):
            raise ValidationError(
                f"重排序阈值必须在 {RAGThresholds.MIN_RERANK_THRESHOLD} 到 {RAGThresholds.MAX_RERANK_THRESHOLD} 之间，当前值: {threshold}"
            )
    
    @staticmethod
    def validate_top_k(top_k: int) -> None:
        """验证top_k参数"""
        if not ThresholdValidator.validate_top_k(top_k):
            raise ValidationError(
                f"top_k必须在 {RAGThresholds.MIN_TOP_K} 到 {RAGThresholds.MAX_TOP_K} 之间，当前值: {top_k}"
            )
    
    @staticmethod
    def validate_chunk_size(chunk_size: int) -> None:
        """验证文本块大小"""
        if not ThresholdValidator.validate_chunk_size(chunk_size):
            raise ValidationError(
                f"文本块大小必须在 {KnowledgeBaseThresholds.MIN_CHUNK_SIZE} 到 {KnowledgeBaseThresholds.MAX_CHUNK_SIZE} 之间，当前值: {chunk_size}"
            )
    
    @staticmethod
    def validate_chunk_overlap(chunk_overlap: int) -> None:
        """验证文本块重叠"""
        if not ThresholdValidator.validate_chunk_overlap(chunk_overlap):
            raise ValidationError(
                f"文本块重叠必须在 {KnowledgeBaseThresholds.MIN_CHUNK_OVERLAP} 到 {KnowledgeBaseThresholds.MAX_CHUNK_OVERLAP} 之间，当前值: {chunk_overlap}"
            )
    
    @staticmethod
    def validate_temperature(temperature: float) -> None:
        """验证温度参数"""
        if not ThresholdValidator.validate_temperature(temperature):
            raise ValidationError(
                f"温度必须在 {ModelThresholds.MIN_TEMPERATURE} 到 {ModelThresholds.MAX_TEMPERATURE} 之间，当前值: {temperature}"
            )
    
    @staticmethod
    def validate_max_tokens(max_tokens: int) -> None:
        """验证最大token数"""
        if not ThresholdValidator.validate_max_tokens(max_tokens):
            raise ValidationError(
                f"最大token数必须在 {ModelThresholds.MIN_MAX_TOKENS} 到 {ModelThresholds.MAX_MAX_TOKENS} 之间，当前值: {max_tokens}"
            )
    
    @staticmethod
    def validate_text_length(text: str) -> None:
        """验证文本长度"""
        if not ThresholdValidator.validate_text_length(text):
            length = len(text)
            raise ValidationError(
                f"文本长度必须在 {EmbeddingThresholds.MIN_TEXT_LENGTH} 到 {EmbeddingThresholds.MAX_TEXT_LENGTH} 之间，当前长度: {length}"
            )
    
    @staticmethod
    def validate_name_length(name: str) -> None:
        """验证名称长度"""
        if not ThresholdValidator.validate_name_length(name):
            length = len(name)
            raise ValidationError(
                f"名称长度必须在 {ValidationThresholds.MIN_NAME_LENGTH} 到 {ValidationThresholds.MAX_NAME_LENGTH} 之间，当前长度: {length}"
            )
    
    @staticmethod
    def validate_description_length(description: str) -> None:
        """验证描述长度"""
        if not ThresholdValidator.validate_description_length(description):
            length = len(description)
            raise ValidationError(
                f"描述长度不能超过 {ValidationThresholds.MAX_DESCRIPTION_LENGTH}，当前长度: {length}"
            )
    
    @staticmethod
    def validate_collection_name_length(name: str) -> None:
        """验证集合名称长度"""
        if not ThresholdValidator.validate_collection_name_length(name):
            length = len(name)
            raise ValidationError(
                f"集合名称长度必须在 {ValidationThresholds.MIN_COLLECTION_NAME_LENGTH} 到 {ValidationThresholds.MAX_COLLECTION_NAME_LENGTH} 之间，当前长度: {length}"
            )
    
    @staticmethod
    def validate_file_extension(filename: str) -> None:
        """验证文件扩展名"""
        if not ThresholdValidator.validate_file_extension(filename):
            import os
            _, ext = os.path.splitext(filename.lower())
            raise ValidationError(
                f"不支持的文件类型: {ext}。支持的类型: {', '.join(SecurityThresholds.ALLOWED_FILE_EXTENSIONS)}"
            )
    
    @staticmethod
    def validate_file_size(file_size_bytes: int) -> None:
        """验证文件大小"""
        if not ThresholdValidator.validate_file_size(file_size_bytes):
            max_size_mb = SecurityThresholds.MAX_FILE_CONTENT_LENGTH / (1024 * 1024)
            min_size_kb = SecurityThresholds.MIN_FILE_SIZE_KB
            current_size_mb = file_size_bytes / (1024 * 1024)
            raise ValidationError(
                f"文件大小必须在 {min_size_kb}KB 到 {max_size_mb}MB 之间，当前大小: {current_size_mb:.2f}MB"
            )
    
    @staticmethod
    def validate_batch_size(batch_size: int) -> None:
        """验证批量大小"""
        if not ThresholdValidator.validate_batch_size(batch_size):
            raise ValidationError(
                f"批量大小必须在 1 到 {EmbeddingThresholds.MAX_BATCH_SIZE} 之间，当前值: {batch_size}"
            )

# 导出验证器
__all__ = [
    'ThresholdValidator',
    'ThresholdValidatorWithError',
    'ValidationError'
] 